#/bin/sh 
if [ "$HOME" = "" ]; then
        HOME="/"; export HOME
fi
#
# Generic launch script for use with MDSS cdrom mechanism 
#
# marc.sacoolas@sun.com 
#
#**********************
#     Begin subs
#**********************

# Check if user wants to be root 
check_root_maybe() {
if [ "$USER" != "root" ]; then
  while true
  do
    echo ""
    echo "You may need to be root user to install this software in certain directories. \c"
    echo ""
    echo "Would you like to become root user? <[y/n] n> \c"
    read ans
     if [ "$ans" = "" ]; then 
	ans="n" 
     fi

     case "$ans" in
      y|Y|yes|YES) echo "";echo "Enter super user \c";su root -c 'USER=root;export USER;$COMPANY_DIR/_install/launchit.sh $theARGS';exit 0
            ;;
      N|Q|n|q|No|quit|NO|QUIT|Quit) break 
            ;;
     esac
  done
fi 
}
# Check if root - manditory
check_root() {
if [ "$USER" != "root" ]; then
  while true
  do
    echo ""
    echo "You need to be super user to install this software. \c"
    echo ""
    echo "Would you like to continue with the installation? <[y/n] y> \c"
    read ans
     if [ "$ans" = "" ]; then 
	ans="y" 
     fi

     case "$ans" in
      y|Y|yes|YES) echo "";echo "Enter super user \c";su root -c 'USER=root;export USER;$COMPANY_DIR/_install/launchit.sh $theARGS';exit 0
            ;;
      N|Q|n|q|No|quit|NO|QUIT|Quit) end_it
            ;;
     esac
  done
fi 
}
# terminate installation
end_it () {
  echo " " 
  echo $1
  echo " " 
  echo "Hit return to end ..."
  echo " "
  read answer
  exit 0 
}
# prompt to remove stuff before terminating 
end_it_rm () {
cd $HOME
echo ""
echo "Remove demo directory $installdir?"
        echo "\n<[y/n] y>\c";read x
        if [ "$x" = "" ];then x="y"
        fi
        if [ $x = "n" -o $x = "no" -o $x = "No" -o $x = "N" ];then
		exit 0
	fi
        if [ $x = "y" -o $x = "yes" -o $x = "Yes" -o $x = "Y" ];then
          rm -rf $installdir
          if [ -d $path ]; then
                echo ""
                echo "Could not remove installation directory."
                echo ""
		end_it
          fi
        else
	  end_it_rm
        fi
}

# if prompt is required, do so
prompt_choice() {
  echo ""
  echo "Which package from Network Engineering Software would you like to install?\n"
  echo ""
  echo "1) Telaxian Shield firewall server" 
  echo "2) Sky Light web server" 
  echo ""
  echo "3) Exit installation."
  echo ""
  echo "Enter your selection:  \c"
  read answer

  case "$answer" in
     1)	WHICHPROD="" 
	WHICHPRODNAME="Team Quest Light" 
        WHICHSCRIPT="install.sh"
	WHICHDIR="install"
	;;
     2) WHICHPROD="install-www-ssl.tar.Z"
	WHICHPRODNAME="Sky Light" 
        WHICHSCRIPT="webinst"
	WHICHDIR="install-www-ssl"
	;;
     3) end_it  
	;;
     *) echo "Please choose one."
	prompt_choice
	;;
  esac
}
# if choice driven by .demosh script:
auto_choice () {
  case "$1" in
     1) WHICHPROD="solidSDK30ssx.tar.Z"
        WHICHPRODNAME="Solid SDK"
        install_script=""
        ;;
     2) WHICHPROD="NotifyServer"
        WHICHPRODNAME="IPnetWATCHER Notification Server"
        installdir=NotificationServer11woJRE
        ;;
  esac   

}
# if system installation is required
prompt_path () {
installdir="$HOME"
echo ""
echo "Please enter an install path to begin installing $WHICHPRODNAME:"

echo "\n<$installdir>\c";read path
if [ -n "$path" ]; then
installdir="$path"
fi
verify_path $installdir
}

verify_path () {
installdir=$1
if [ ! -d $installdir ]; then
        echo "$installdir does not exist, create it?"
	echo "\n<[y/n] y>\c";read x
	if [ "$x" = "" ];then x="y"
	fi
	if [ $x = "y" -o $x = "yes" -o $x = "Yes" -o $x = "Y" ];then 
          mkdir -p $installdir
          if [ ! -d $installdir ]; then
		echo ""
                echo "Could not create destination directory."
                prompt_path 
          fi
	else
	  end_it "A destination directory needs to be specified."
	fi
fi
if [ ! -w $installdir ];then
	echo "You can not write to $installdir" 
	prompt_path
fi

FREE_SPACE=0
df=/tmp/FREE.$$
/usr/bin/df -k $installdir | sed /kbytes/d | sed -e '3,$d' | sed -e '1,$s/  */ /g' > $df
case `/usr/bin/wc -l $df | sed -e 's/^ *//' | cut -f1 -d" " ` in
        1) FREE_SPACE=`cat $df | cut -f4 -d" "` ;;
        2) FREE_SPACE=`cat $df | sed 1d | sed 's/^ *//' | cut -f3 -d" "` ;;
esac
if [ -n $FREE_SPACE ]
then
        if [ $FREE_SPACE -lt $PRODUCT_SIZE ]
        then
           echo
           echo "The product installation requires $PRODUCT_SIZE kbytes of"
           echo "free disk space. There is only $FREE_SPACE kbytes of free"
           echo "disk space in the selected filesystem.  Please free up some"
           echo "disk space, or choose another directory."
                prompt_path
        fi
fi
/bin/rm $df 2>/dev/null

return 0 
}

#*****************
#  end subs
#*****************

#Variables
theARGS=$1
PRODUCT_SIZE='1000'
WHICHPROD=
WHICHPRODNAME=
installdir=$COMPANY_DIR/_install
export PRODUCT_SIZE WHICHPROD WHICHPRODNAME installdir theARGS

  echo ""
  echo 

##  Call out Subs here ###

#check_root
#check_root_maybe
auto_choice $theARGS
#prompt_choice
prompt_path

# below is custom for each demo

echo ""
echo "$WHICHPRODNAME installation starting..."
echo ""
verify_path $installdir
cd $installdir
zcat $COMPANY_DIR/_install/$WHICHPROD | tar xvf -
echo ""
echo ""
echo "Please see the readme.txt file for further instructions."
#./$install_script 
end_it
